import 'package:flutter_wan_android/data/base_data_layer_api.dart';
import 'package:flutter_wan_android/data/user/user_entity.dart';
import 'package:flutter_wan_android/data/user/user_local_api.dart';
import 'package:flutter_wan_android/data/user/user_network_api.dart';

import '../logic_result.dart';

class UserRepository {
  final UserNetworkApi _network;
  final UserLocalApi _local;

  UserRepository(this._network, this._local);

  Future<LogicResult<dynamic>> login(String account, String password) async {
    return performLogic(() async {
      UserEntity user = await _network.login(account, password);
      _local.loginSuccess(user);
      return user;
    });
  }

  Future<bool> getLoginState() async {
    return await _local.getLoginState();
  }

  Future<LogicResult<void>> register(
    String account,
    String password,
    String rePassword,
  ) async {
    return performLogic(
      () async => await _network.register(account, password, rePassword),
    );
  }
}
